﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Announcement.DAL.DataSetApplyMgrTableAdapters;

namespace Announcement
{
    public partial class ApplyList : LoginPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lvApplyMgr_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            HiddenField hfId = (HiddenField)e.Item.FindControl("hfId");
            if (e.CommandName == "Read")
            {
                Response.Redirect("ApplyRead.aspx?Id=" + hfId.Value);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.dBind();
        }

        private void dBind()
        {
            string cCode = txtCCode.Text;
            string local = txtLocal.Text;
            string typeName = ddlApplyType.SelectedItem.Text;
            if (typeName == "请选择")
            {
                typeName = "";
            }
            DateTime startDate = DateTime.Parse("1753-01-01");
            DateTime endDate = DateTime.Parse("9999-12-31");

            if (txtStartDate.Text.Length > 0 && txtEndDate.Text.Length > 0)
            {
                startDate = DateTime.Parse(txtStartDate.Text);
                endDate = DateTime.Parse(txtEndDate.Text);
            }

            var applyMgrRow = new ApplyMgrTableAdapter();
            DataTable dt = new DataTable();
            //输入框全部为空时，查询所有数据
            if (cCode.Length == 0 && local.Length == 0 && typeName.Length == 0
                && txtStartDate.Text.Length == 0 && txtEndDate.Text.Length == 0)
            {
                dt = applyMgrRow.GetApplyMgr();
            }
            else
            {
                dt = applyMgrRow.SearchMgr(cCode, local, typeName, startDate, endDate);
            }

            lvApplyMgr.Items.Clear();
            lvApplyMgr.DataSourceID = null;
            lvApplyMgr.DataSource = dt;
            lvApplyMgr.DataBind();
        }

        protected void lvApplyMgr_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            DataPager dp = (DataPager)lvApplyMgr.FindControl("DataPager1");
            dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            this.dBind();
        }

        protected void ddlApplyType_DataBound(object sender, EventArgs e)
        {
            ListItem li = new ListItem("请选择");
            ddlApplyType.Items.Insert(0, li);
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session["UserName"] = null;
            Response.Redirect("~/Login.aspx");
        }
    }
}
